package com.jsh.erp.service.gongxu;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.constants.ExceptionConstants;
import com.jsh.erp.datasource.entities.*;
import com.jsh.erp.datasource.mappers.*;
import com.jsh.erp.exception.BusinessRunTimeException;
import com.jsh.erp.exception.JshException;
import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 工序的业务层
 */
@Service
public class GongxuService {
    private Logger logger = LoggerFactory.getLogger(GongxuService.class);

    @Resource
    private GongxuMapper gongxuMapper;

    @Resource
    private GongxuMapperEx gongxuMapperEx;
    @Resource
    private LogService logService;

    public Gongxu getGongxu(long id)throws Exception {
        Gongxu result=null;
        try{
            result=gongxuMapper.selectByPrimaryKey(id);
        }catch(Exception e){
            JshException.readFail(logger, e);
        }
        return result;
    }

    public List<Gongxu> getGongxuListByIds(String ids)throws Exception {
        List<Long> idList = StringUtil.strToLongList(ids);
        List<Gongxu> list = new ArrayList<>();
        try{
            GongxuExample example = new GongxuExample();
            example.createCriteria().andIdIn(idList);
            list = gongxuMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e);
        }
        return list;
    }

    public List<Gongxu> getGongxu()throws Exception {
        GongxuExample example = new GongxuExample();
        example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        List<Gongxu> list=null;
        try{
            list=gongxuMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e);
        }
        return list;
    }

    public List<Gongxu> select(String name,int offset, int rows)throws Exception {
        List<Gongxu> list=null;
        try{
            list=gongxuMapperEx.selectByConditionGongxu(name,offset, rows);
        }catch(Exception e){
            JshException.readFail(logger, e);
        }
        return list;
    }

    public Long countGongxu(String name)throws Exception {
        Long result=null;
        try{
            result=gongxuMapperEx.countsByGongxu(name);
        }catch(Exception e){
            JshException.readFail(logger, e);
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insert(String beanJson, HttpServletRequest request)throws Exception {
        Gongxu gongxu = JSONObject.parseObject(beanJson, Gongxu.class);
        int result=0;
        try{
            result=gongxuMapper.insertSelective(gongxu);
            logService.insertLog("经手人",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(gongxu.getName()).toString(), request);
        }catch(Exception e){
            JshException.writeFail(logger, e);
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int update(String beanJson, Long id, HttpServletRequest request)throws Exception {
        Gongxu gongxu = JSONObject.parseObject(beanJson, Gongxu.class);
        gongxu.setId(id);
        int result=0;
        try{
            result=gongxuMapper.updateByPrimaryKeySelective(gongxu);
            logService.insertLog("经手人",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(gongxu.getName()).toString(), request);
        }catch(Exception e){
            JshException.writeFail(logger, e);
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int delete(Long id, HttpServletRequest request)throws Exception {
        int result=0;
        try{
            result=gongxuMapper.deleteByPrimaryKey(id);
            logService.insertLog("经手人",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_DELETE).append(id).toString(), request);
        }catch(Exception e){
            JshException.writeFail(logger, e);
        }
        return result;
    }
    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int deletes(String ids, HttpServletRequest request)throws Exception {
        int result=0;
        String[] idArr = ids.split(",");
        try{
            result=gongxuMapperEx.batchByIds(idArr);
            logService.insertLog("经手人",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_DELETE).append(ids).toString(), request);
        }catch(Exception e){
            JshException.writeFail(logger, e);
        }
        return result;
    }
    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDelete(String ids, HttpServletRequest request) throws Exception{
        List<Long> idList = StringUtil.strToLongList(ids);
        GongxuExample example = new GongxuExample();
        example.createCriteria().andIdIn(idList);
        int result=0;
        try{
            result= gongxuMapper.deleteByExample(example);
            logService.insertLog("经手人", "批量删除,id集:" + ids, request);
        }catch(Exception e){
            JshException.writeFail(logger, e);
        }
        return result;
    }

    public int checkIsNameExist(Long id, String name) throws Exception{
        GongxuExample example = new GongxuExample();
        example.createCriteria().andIdNotEqualTo(id).andNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        List<Gongxu> list =null;
        try{
            list=gongxuMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e);
        }
        return list==null?0:list.size();
    }

    public String getGongxuByIds(String personIDs)throws Exception {
        List<Long> ids = StringUtil.strToLongList(personIDs);
        GongxuExample example = new GongxuExample();
        example.createCriteria().andIdIn(ids).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        example.setOrderByClause("id asc");
        List<Gongxu> list =null;
        try{
            list=gongxuMapper.selectByExample(example);
        }catch(Exception e){
            JshException.readFail(logger, e);
        }
        StringBuffer sb = new StringBuffer();
        if (null != list) {
            for (Gongxu gongxu : list) {
                sb.append(gongxu.getName() + " ");
            }
        }
        return  sb.toString();
    }
}
